﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongHopBookFlightDAO.DAO
{
    public class ChuyenBayDAO
    {
        public static List<ChuyenBay> GetAll()
        {
            List<ChuyenBay> listObjResults = new List<ChuyenBay>();
            try
            {
                BookFlightDataContext context = new BookFlightDataContext();
                listObjResults = context.ChuyenBays.ToList<ChuyenBay>();
            }
            catch { }
            return listObjResults;
        }

        public static int Insert(ChuyenBay _obj)
        {
            int ID = -1;
            try
            {
                BookFlightDataContext context = new BookFlightDataContext();
                context.ChuyenBays.InsertOnSubmit(_obj);
                context.SubmitChanges();
                ID = _obj.ChuyenBayID;
            }
            catch { }
            return ID;
        }

        public static List<ChuyenBay> GetFlights(string maDiemDi, string maDiemDen, DateTime ngayDi)
        {
            DiaDiem diemDi = DiaDiemDAO.SelectByCode(maDiemDi);
            DiaDiem diemDen = DiaDiemDAO.SelectByCode(maDiemDen);

            List<ChuyenBay> listObjResults = new List<ChuyenBay>();
            try
            {
                BookFlightDataContext context = new BookFlightDataContext();
                listObjResults = context.ChuyenBays.Where(n => n.DiemDiID == diemDi.DiaDiemID && n.DiemDenID == diemDen.DiaDiemID && n.NgayDi.Value.Date== ngayDi.Date).ToList();
            }
            catch { }

            return listObjResults;
        }

        public static ChuyenBay SelectFlightByFlightCode(string maChuyenBay)
        {
            ChuyenBay chuyenBay = null;
            try
            {
                BookFlightDataContext context = new BookFlightDataContext();
                chuyenBay = context.ChuyenBays.Where(p => p.MaChuyenBay.Trim() == maChuyenBay.Trim()).FirstOrDefault();

            }
            catch { }
            return chuyenBay;
        }
    }
}
